<?php
/**
 *	FILENAME: 		/server_app/authenticate_ldap.php
 *	DESCRIPTION:	This script creates a valid JSON response after authenticating a user.
 *	AUTHOR:			Taylor Clifton 
 *	VERSION:		1.1.0
 *	LAST MODIFIED:	12/3/10
 **/

require_once($_SERVER['DOCUMENT_ROOT'] . "/config/environment.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/functions.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/ldap.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/logging.php");

enforceSSL();
setRequestParameters();

$userName = $_PARAMETERS["username"];
$password = $_PARAMETERS["password"];

if (strlen($userName) == 0 || strlen($password) == 0) {
    // Inproper Data
    $response["response_type"] = "error";
    $response["error_desc"] = "Username or Password Left Blank.";
    logWarning("No user data supplied during authentication");
    echo(json_encode($response));
} else if (authenticateLDAPUser($userName, $password)) {
    // Authenticated
    $response["response_type"] = "success";
    logAction("User Authenticated");
    echo(json_encode($response));
} else {
    // Not Authenticaed
    $response["response_type"] = "error";
    $response["error_desc"] = "Invalid Username and Password.";
    logWarning("User Failed Authentication");
    echo(json_encode($response));
}
